package com.experiment.experiment_big.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.experiment.experiment_big.entity.User;
import com.experiment.experiment_big.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
@Service(value = "my")
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",username);
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }else{
            List<GrantedAuthority> ans = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRole());
            return new org.springframework.security.core.userdetails.User(user.getUserId(),user.getPassword(),ans);
        }
    }
}
